package com.qiuling.iot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author：zhangqiang
 * @Description：IOT客户端线程池
 * @Date：Created in 15:32 2020/2/23
 * @Modified By：
 */
@Configuration
public class ThreadPoolConfigurer {

    /**
     * 初始化he客户端连接池（连接使用）
     * @return
     */
    @Bean(name = "clientTaskPool")
    public ThreadPoolTaskExecutor clientTaskPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setKeepAliveSeconds(60);
        executor.setMaxPoolSize(Integer.MAX_VALUE);
        executor.setThreadNamePrefix("clientTaskPool");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

    /**
     * 初始化客户端消息池（发送消息使用）
     * @return
     */
    @Bean(name = "clientMessageTaskPool")
    public ThreadPoolTaskExecutor clientMessageTaskPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setKeepAliveSeconds(60);
        executor.setMaxPoolSize(Integer.MAX_VALUE);
        executor.setThreadNamePrefix("clientMessageTaskPool");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
